package xyz.quanguanjia.masterboy.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import xyz.quanguanjia.masterboy.MBDApplication;
import xyz.quanguanjia.masterboy.MBDHttpClient;
import xyz.quanguanjia.masterboy.MBDIntegrationAdapter;
import xyz.quanguanjia.masterboy.MBDIntegrationDetailModel;
import xyz.quanguanjia.masterboy.MBDModelResponse;
import xyz.quanguanjia.masterboy.MBDToolbarUtils;
import xyz.quanguanjia.masterboy.R;
import xyz.quanguanjia.masterboy.ServiceType;

public class MBDIntegrateActivity extends AppCompatActivity {
    private List<MBDIntegrationDetailModel> integrationDetails;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integrate);

        Toolbar toolbar = findViewById(R.id.toolbar);
        MBDToolbarUtils.setCenterTitle(toolbar, "积分");

        // 设置返回按钮点击事件，点击时直接调用 finish() 方法关闭当前页面
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });


        recyclerView = findViewById(R.id.recyclerView);

        Button eranIntegrateButton = (Button) findViewById(R.id.btn_earn_integrate);
        eranIntegrateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MBDIntegrateActivity.this, MBDEarnIntegrateActivity.class);
                startActivity(intent);
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(MBDApplication.getContext(), LinearLayoutManager.VERTICAL));

        // 初始化 integrationDetails 数据，可以从数据库或其他数据源中获取
        refreshUI();
    }


    // 在 MBDIntegrateActivity 中更新 fetchDataFromApi 方法
    private void refreshUI() {
        doRequest("1", new RequestCallback() {
            @Override
            public void onSuccess(List<MBDIntegrationDetailModel> integrationDetails) {
                MBDIntegrationAdapter adapter = new MBDIntegrationAdapter(integrationDetails);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Error error) {
                Toast.makeText(MBDIntegrateActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

    }

    private void doRequest(String pageNum,RequestCallback callback){
        if (TextUtils.isEmpty(pageNum)) {
            throw new AssertionError("Assertion failed:  pageNum is Empty" );
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("pageNum",pageNum);
        // 添加请求参数

        // 使用新的 SERVICE_INTEGRATE_DETAIL 服务类型
        MBDHttpClient.shared().doRequest(ServiceType.SERVICE_INTEGRATE_DETAIL, params, new MBDHttpClient.Callback() {
            @Override
            public void completion(boolean success, Error error, MBDModelResponse response) {
                if (success) {
                    // 处理成功的响应
                    // response 中包含了请求成功后的数据
                    LinkedTreeMap<String,Object> data = (LinkedTreeMap<String,Object>) response.data;

                    String integral = (String) data.get("myIntegral") + "";
                    TextView textView = findViewById(R.id.tv_current_integrate);
                    textView.setText(integral);

                    LinkedTreeMap<String,Object> pageDataInfo = (LinkedTreeMap<String,Object>) data.get("pageDataInfo");
                    ArrayList list = (ArrayList) pageDataInfo.get("data");

                    ArrayList resultArrayList = new ArrayList<MBDIntegrationDetailModel>();
                    for (int i = 0; i < list.size(); i++) {
                        LinkedTreeMap<String,Object> linkedTreeMap = (LinkedTreeMap<String, Object>) list.get(i);
                        Gson gson = new Gson();
                        String jsonString = gson.toJson(linkedTreeMap);
                        MBDIntegrationDetailModel detail= gson.fromJson(jsonString, MBDIntegrationDetailModel.class);
                        resultArrayList.add(detail);
                    }
                    callback.onSuccess(resultArrayList);

                } else {
                    // 处理失败的情况
                    callback.onFailure(error);
                }
            }
        });
    }




    private interface RequestCallback {
        void onSuccess(List<MBDIntegrationDetailModel> integrationDetails);
        void onFailure(Error error);
    }
}

